<%- include('head') -%>

  <body>
    <div>
      <span class="layui-breadcrumb">
        <a href="/dashboard">首页</a>
        <a href="/itemLists">商品管理</a>
        <a><cite>更新商品</cite></a>
      </span>
    </div>
    <form class="layui-form" action="" style="margin-top:20px;max-width: 800px">
      <div class="layui-form-item">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-block">
          <input type="hidden" name="id" value="<%= item.id %>">
          <input type="text" value="<%= item.itemName %>" name="itemName" required lay-verify="required" placeholder="请输入商品名称" autocomplete="off"
            class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-inline">
          <input type="number" value="<%= item.itemPrice %>" name="itemPrice" required lay-verify="required" placeholder="请输入价格" autocomplete="off"
            class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">辅助文字</div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">库存</label>
        <div class="layui-input-inline">
          <input type="number" value="<%= item.itemStore %>" name="itemStore" placeholder="请输入库存" autocomplete="off" value="0" class="layui-input">
        </div>

      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">上下架</label>
        <div class="layui-input-block">
          <input type="checkbox" <%= item.onSale== '0'? 'checked': '' %> name="onSale" lay-skin="switch">
        </div>
      </div>

      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">商品描述</label>
        <div class="layui-input-block">
          <textarea name="itemDesc" placeholder="请输入内容" class="layui-textarea"><%= item.itemDesc %></textarea>
        </div>
      </div>
      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">商品封面</label>
        <div class="layui-input-block">
          <button type="button" class="layui-btn" id="upload">
            <i class="layui-icon">&#xe67c;</i>上传图片
          </button>
          <img src="<%= item.thumb %>" id="img" alt="">
        </div>
      </div>
      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">商品详情</label>
        <div class="layui-input-block">
          <div id="content">
           
          </div>
        </div>
      </div>
      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="addItem">立即提交</button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
    </form>
    <script src="/static/layui/layui.js"></script>
    <script src="/static/js/wangEditor.js"></script>
    <script>
      layui.use(['form', 'jquery', 'layer', 'upload'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;
        var upload = layui.upload;
        // 封面图片初始值
        var thumb = '<%= item.thumb %>';
        // 初始化商品组件
        //执行实例
        var uploadInst = upload.render({
          elem: '#upload' //绑定元素
          ,field:'file'
          , url: '/upload/' //上传接口
          , done: function (res) {
            //上传完毕回调
            console.log(res);
            thumb = res.data.imgUrl;
            $('#img').attr('src', thumb)
          }
          , error: function () {
            //请求异常回调
            console.log('上传失败');
          }
        });
        // 初始化 富文本编辑器
        var E = window.wangEditor;
        var editor = new E('#content');
        editor.create()
        // 定义富文本初始值
        editor.txt.html('<%-item.content%>');
        //监听提交
        form.on('submit(addItem)', function (data) {
          
          // 提交的参数
         var params = data.field;
          if (params.onSale) {
            params.onSale = '0';
          } else {
            params.onSale = '1';
          }
          params.thumb = thumb;
          params.content = editor.txt.html();
       
          // 增加商品
          
          delete params.file;
          console.log(params);
          $.ajax({
            url: '/itemUpdate',
            type: 'post',
            data: params,
            success: function (res) {
              if (res.code === 0) {
                layer.msg(res.msg, {
                  icon: 1,
                  time: 1000
                }, function () {
                  // 关闭后
                  // 回到商品列表页
                  location.href="/itemLists"
                })
              }
            }
          })
          return false;
        });
      });
    </script>
  </body>

  </html>